<?php
class OptionsController extends Controller
{	
	public function editAction()
	{
		$optionsDir = $this->optionsDir;
		
		if ($attributes = $this->request->getPost('options'))
		{
			foreach ($attributes as $key => $value)
			{
				$model = new OptionsModel();
				$model->findByAttributes(array('option_key' => $key));
				$model->option_key = $key;
				$model->option_value = $value;
				$model->save(false);
			}
			
			$this->createOptionsFile($optionsDir);
		}
		
		$this->createOptionsFile($optionsDir, true);
		$this->view->options = require $optionsDir;
	}
	
	protected function createOptionsFile($optionsDir, $bool = false)
	{
		if ($bool && file_exists($optionsDir))
			return ;
		
		$attributes = array();
		$models = OptionsModel::model()->findAll();
		foreach ($models as $model)
			$attributes[$model->option_key] = $model->option_value;
		
		$html = '<?php return ' . var_export($attributes, true) . ';?>';
		file_put_contents($optionsDir, $html);
	}
}
